package com.future.mayi.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.SystemMessageAdapter;
import com.future.mayi.app.App;
import com.future.mayi.dialog.KefuDialog;
import com.future.mayi.model.KefuModel;
import com.future.mayi.model.MessageModel;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.KefuResponse;
import com.future.mayi.response.ListMessageResponse;
import com.future.mayi.response.MsgDetailResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.OnClick;

public class SystemMessageActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener, OnItemClickListener {

    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.iv_phone)
    ImageView ivPhone;
    @BindView(R.id.tv_message_count)
    TextView tvMessageCount;

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;

    private SystemMessageAdapter adapter;

    private int page = 0;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("消息");
        getRightText().setText("清空");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_system_message);
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new SystemMessageAdapter(this, recyclerView);
        recyclerView.setAdapter(adapter);
    }


    private String phone;
    private KefuModel models;

    @Override
    protected void initDates() {
        new HttpRequest<KefuResponse>()
                .with(this)
                .setApiCode(ApiCst.getKfrx)
                .setListener(new HttpRequest.OnNetworkListener<KefuResponse>() {
                    @Override
                    public void onSuccess(KefuResponse response) {
                        if (response.models == null) return;
                        models = response.models;
                        phone = response.models.getTelephone().getTel();
                    }

                    @Override
                    public void onFail(String message, KefuResponse response) {
                        toast(message);
                    }
                })
                .start(new KefuResponse());
        recyclerView.refresh();
    }

    @Override
    protected void initListener() {
        getRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                readAllMsg();
            }
        });
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }


    @OnClick({R.id.iv_message, R.id.iv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_message: {
                if (models == null || models.getWeixin() == null) {
                    initDates();
                    toast("正在获取客服微信");
                    return;
                }
                new KefuDialog(this).show(0);
            }
            break;
            case R.id.iv_phone: {
                if (TextUtils.isEmpty(phone)) {
                    initDates();
                    toast("正在获取客服电话");
                    return;
                }
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            break;
        }
    }


    private void getData() {
        new HttpRequest<ListMessageResponse>()
                .with(this)
                .setApiCode(ApiCst.getMsgList)
                .addParam("token", App.getToken(mContext))
                .addParam("p", page)
                .setListener(new HttpRequest.OnNetworkListener<ListMessageResponse>() {
                    @Override
                    public void onSuccess(ListMessageResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.list, page, 10);
                    }

                    @Override
                    public void onFail(String message, ListMessageResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getApplicationContext(), message);
                    }
                }).start(new ListMessageResponse());
    }


    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }


    @Override
    public void OnItem(View view, int position) {
        MessageModel model = adapter.getItem(position);
        if (model == null) return;
        model.setIs_read(1);
        adapter.notifyDataSetChanged();
        //处理跳转
        //消息类型 0-图文 1-单品 2-活动会场 3-订单
        //relation_id
        switch (model.getType()) {
            case 0:
                MsgDetailActivity.startActivity(this, String.valueOf(model.getMsg_id()));
                break;
            case 1:
                GoodsActivity.startActivity(this, model.getRelation_id());
                break;
            case 2:
                ActivitiesActivity.startActivity(this, model.getRelation_id());
                break;
            case 3:
                GoodsOrderActivity.startActivity(this, String.valueOf(model.getRelation_id()));
                break;
        }
        if (model.getType() != 0)
            detailMsg(model.getMsg_id());
    }


    private void detailMsg(int msg_id) {
        new HttpRequest<MsgDetailResponse>()
                .with(this)
                .setApiCode(ApiCst.detailMsg)
                .addParam("token", App.getToken(mContext))
                .addParam("msg_id", msg_id)
                .setListener(new HttpRequest.OnNetworkListener<MsgDetailResponse>() {
                    @Override
                    public void onSuccess(MsgDetailResponse response) {
                    }

                    @Override
                    public void onFail(String message, MsgDetailResponse response) {
                    }
                }).start(new MsgDetailResponse());
    }

    private void readAllMsg() {
        if (adapter.getItemCount() == 0) return;
        showLoadingDialog();
        new HttpRequest<ListMessageResponse>()
                .with(this)
                .setApiCode(ApiCst.readAllMsg)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<ListMessageResponse>() {
                    @Override
                    public void onSuccess(ListMessageResponse response) {
                        dissmissLoadingDialog();
                        for (MessageModel messageModel : adapter.getList()) {
                            if (messageModel == null) continue;
                            messageModel.setIs_read(1);
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFail(String message, ListMessageResponse response) {
                        dissmissLoadingDialog();
                        toast(message);
                    }
                }).start(new ListMessageResponse());
    }


    @Override
    public void OnLongItem(View view, int position) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


}
